package com.fhz.okhttptest;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        Login login = new Login();
//        this.getLifecycle().addObserver(login);
//
//        MutableLiveData<String> stringMutableLiveData = new MutableLiveData<>();
//        stringMutableLiveData.observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(String s) {
//                //
//            }
//        });
//
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("https://api.github.com/")
//                .client(new OkHttpClient().newBuilder()
//                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//                        .build())
//                .addConverterFactory(GsonConverterFactory.create()) //string  User.
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .build();
//
//        GitHubService service = retrofit.create(GitHubService.class);
//
//
//        service.listRepos("name");
//
//        Call<List<Repo>> repos = service.listRepos("octocat");
//
//        repos.enqueue(new retrofit2.Callback<List<Repo>>() {
//            @Override
//            public void onResponse(Call<List<Repo>> call, retrofit2.Response<List<Repo>> response) {
//                System.out.println("成功");
//            }
//
//            @Override
//            public void onFailure(Call<List<Repo>> call, Throwable t) {
//
//            }
//        });
//
////        service.listRepos1("小明")
////                .subscribeOn()
////                .observeOn()
////                .subscribe(new Observer<List<Repo>>() {
////                    @Override
////                    public void onSubscribe(Disposable d) {
////
////                    }
////
////                    @Override
////                    public void onNext(List<Repo> repos) {
////                        //网络返回值
////                    }
////
////                    @Override
////                    public void onError(Throwable e) {
////                        //清楚错误
////                    }
////
////                    @Override
////                    public void onComplete() {
////
////                    }
////                });
    }

    private void extracted() {

    Cache cache = new Cache(new File(""),1000);

    OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
//                .addInterceptor("")
//                .cache(cache)
                .build();
        Request request = new Request.Builder()
                .url("http://43.143.146.165:7777/goods/info?category_id=0&currentPage=1&pageSize=10")
                .build();



        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                System.out.println("失败");
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                System.out.println(response.body().string());
//            }
//        });
//        try {
//            Response execute = call.execute(); //卡住.->  线程
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }

    //同步的方式
    private void extracted(Call call) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response execute = call.execute();
                    System.out.println(1111);
                    System.out.println(execute.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }).start();
    }
}